package com.xuecheng.content.api;

import com.alibaba.fastjson.JSON;
import com.xuecheng.base.model.RestResponse;
import com.xuecheng.content.model.dto.CourseBaseInfoDto;
import com.xuecheng.content.model.dto.CoursePreviewDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.CoursePublish;
import com.xuecheng.content.service.CoursePublishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Api("课程发布相关接口")
@Controller
public class CoursePublishController {
    @Autowired
    CoursePublishService coursePublishService;

    @ApiOperation("课程预览接口")
    @GetMapping("/coursepreview/{courseId}")
    public ModelAndView coursePreview(@PathVariable Long courseId){
        CoursePreviewDto dto = coursePublishService.getCoursePreviewDto(courseId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("model",dto);
        modelAndView.setViewName("course_template");
        return modelAndView;
    }

    @ApiOperation("课程提交审核")
    @ResponseBody
    @PostMapping("/courseaudit/commit/{courseId}")
    public RestResponse<String> courseCommitAudit(@PathVariable Long courseId){
        Long companyId = 1232141425L;
        return coursePublishService.courseCommitAudit(companyId,courseId);
    }

    @ApiOperation("课程审核功能")
    @ResponseBody
    @PostMapping("/course/audit/{courseId}")
    public RestResponse<Boolean> courseAudit(@PathVariable("courseId")Long courseId,
                                             boolean pass){
        return coursePublishService.courseAudit(courseId,pass);
    }

    @ApiOperation("课程发布")
    @ResponseBody
    @PostMapping("/coursepublish/{courseId}")
    public RestResponse<Boolean> coursePublish(@PathVariable Long courseId){
        Long companyId = 1232141425L;
        return coursePublishService.coursePublish(companyId,courseId);
    }

    @ApiOperation("查询课程发布信息接口")
    @ResponseBody
    @GetMapping("/r/coursepublish/{courseId}")
    public CoursePublish getCoursePublish(@PathVariable Long courseId){
        return coursePublishService.getCoursePublishById(courseId);
    }

    @ApiOperation("获取课程预览发布信息")
    @ResponseBody
    @GetMapping("/course/whole/{courseId}")
    public CoursePreviewDto getCoursePreviewDto(@PathVariable Long courseId){
        CoursePublish coursePublish = coursePublishService.getCoursePublishById(courseId);
        CoursePreviewDto previewDto = new CoursePreviewDto();
        if(coursePublish == null){
            return previewDto;
        }
        CourseBaseInfoDto dto = new CourseBaseInfoDto();
        BeanUtils.copyProperties(coursePublish,dto);
        List<TeachplanDto> teachplanDtos = JSON.parseArray(coursePublish.getTeachplan(),
                TeachplanDto.class);
        previewDto.setCourseBase(dto);
        previewDto.setTeachplans(teachplanDtos);
        return previewDto;
    }
}
